package com.han.gift.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.han.gift.clients.LoginUserClient;
import com.han.gift.common.exception.Erroenum.ErrorCode;
import com.han.gift.common.exception.ass.GiftAssert;
import com.han.gift.common.util.AjaxResult;
import com.han.gift.common.util.PageList;
import com.han.gift.common.util.encrypt.MD5;
import com.han.gift.dao.EmployeeDao;
import com.han.gift.dao.MealDao;
import com.han.gift.dao.TenantDao;
import com.han.gift.dto.TenantDto;
import com.han.gift.entity.Employee;
import com.han.gift.entity.LoginUser;
import com.han.gift.entity.Tenant;
import com.han.gift.service.TenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * (Tenant)表服务实现类
 *
 * @author makejava
 * @since 2023-02-25 00:50:30
 */
@Service("tenantService")
public class TenantServiceImpl extends ServiceImpl<TenantDao, Tenant> implements TenantService {


    @Autowired
    TenantDao tenantDao;

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    MealDao mealDao;

    @Autowired
    LoginUserClient loginUserClient;

    @Override
    public PageList getPageList(Page<Tenant> page, String keyword) {

        List<Tenant> tenants = tenantDao.loadPageList(page, keyword);

        PageList pageList = new PageList<>(page.getTotal(), tenants);

        return pageList;
    }

    @Override
    @Transactional
    public AjaxResult shopRegister(TenantDto dto) {

        Tenant tenant = dto.getTenant();
        Employee employee = dto.getEmployee();
        Long mealId = dto.getMealId();
        String comfirmPwd = dto.getComfirmPwd();

        //参数校验
//        if (StrUtil.isBlank(tenant.getCompanyName()) || Objects.isNull(mealId) || StrUtil.isBlank(tenant.getCompanyName()) || StrUtil.isBlank(tenant.getAddress())) {
//            throw new RuntimeException("参数不能为空");
//        }
//        if (StrUtil.isBlank(employee.getUsername()) || StrUtil.isBlank(employee.getEmail()) || StrUtil.isBlank(employee.getUsername()) ||
//                StrUtil.isBlank(employee.getTel()) || StrUtil.isBlank(employee.getPassword()) || StrUtil.isBlank(comfirmPwd)) {
//            throw new RuntimeException("参数不能为空");
//        }
//
//        if (!employee.getPassword().equals(comfirmPwd)) {
//            throw new RuntimeException("确认密码不一致");
//        }


        //判断管理员受否存在
        LambdaQueryWrapper<Employee> employeeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        employeeLambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee admin = employeeDao.selectOne(employeeLambdaQueryWrapper);

        GiftAssert.assertObjectIsNull(admin, ErrorCode.CODE_500_SYSTEM_ERROR_ADMIN_EXIST);
//        if(ObjectUtil.isNotNull(admin)){
//            throw new RuntimeException("管理员已存在,请登录后自行添加租户");
//        }

        //判断租户是否存在
        LambdaQueryWrapper<Tenant> tenantLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tenantLambdaQueryWrapper.eq(Tenant::getCompanyName,tenant.getCompanyName());
        Tenant selectOne = tenantDao.selectOne(tenantLambdaQueryWrapper);
        GiftAssert.assertObjectIsNull(selectOne,ErrorCode.CODE_500_SYSTEM_ERROR_TENANT_EXIST);
//        if(ObjectUtil.isNotNull(selectOne)){
//            throw new RuntimeException("店铺已入驻");
//        }

        //审核


        //远程调用auth服务保存登录用户信息和套餐信息并返回login_id--LoginUser(登录用户信息 套餐)  //t_login_user (登录) t_user_meal
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(employee.getUsername());
        loginUser.setPassword(employee.getPassword());
        loginUser.setEmail(employee.getEmail());
        loginUser.setTel(employee.getTel());
        loginUser.setType(0);



        AjaxResult ajaxResult = loginUserClient.addOrUpdate(loginUser);
        if (ajaxResult.isSuccess()) {
            Long longUserId = Long.valueOf(ajaxResult.getResultObj().toString());
            //保存管理员信息 login_id //t_employee
            employee.setRealName(employee.getUsername());
            employee.setInputTime(new Date());
            employee.setState(0);
            employee.setType(1);
            employee.setLoginId(longUserId);
            employeeDao.insert(employee);
            //保存租户信息 admin_id //T_tenant
            tenant.setRegisterTime(new Date());
            tenant.setState(0);
            tenant.setAdminId(employee.getId());

            tenantDao.insert(tenant);


            //添加套餐
            mealDao.saveTenantMeal(mealId,tenant.getId(),new Date(),0);


            //private Long tenantId;
            employee.setTenantId(tenant.getId());

            employeeDao.updateById(employee);

            return AjaxResult.me().setMessage("操作成功").setSuccess(true);
        }
        return AjaxResult.me().setSuccess(false).setMessage("操作失败");
    }
}

